#!/usr/bin/python
# -*- coding: UTF-8 -*-

################################################################################
#
# Copyright (c) 2020 openEuler.org, Inc. All Rights Reserved
#
################################################################################
"""
The maintainer for crawling yaml data

Authors: xiaojianghui
Date:    10/22/2020 11:01 AM
"""

import requests
import lxml.etree as etree
import dbConnecttion.MysqlConn as MysqlConn
import time


class Command(object):
    """
        download sig
    """

    def __init__(self):
        """
        init object
        """
        pass

    @staticmethod
    def handle():
        """
        download detail
        """
        url = 'https://gitee.com/openeuler/community/tree/master/sig'
        try:
            r = requests.get(url)
        except requests.exceptions.ConnectionError:
            print('ConnectionError')
            return
        except requests.exceptions.ChunkedEncodingError:
            print('ChunkedEncodingError')
            return
        html = etree.HTML(r.content)
        if len(html):
            sigs_list = []
            i = 3
            mysql = MysqlConn.Mysql()
            while True:
                sig_name = html.xpath("//div[@id='tree-slider']/div[{}]/div[1]/a/@title".format(i))[0]
                if sig_name == 'sigs.yaml':
                    break
                sql = "select * from cve_git_repo_groups where group_name = %s"
                val = (sig_name,)
                flag = mysql.getOne(sql, val)
                if not flag:
                    print("insert data:{}".format(sig_name))
                    sql = "insert into cve_git_repo_groups (group_name) values (%s)"
                    val = (sig_name,)
                    mysql.insertOne(sql, val)
                    mysql.dispose()
                sig_page = html.xpath("//div[@id='tree-slider']/div[{}]/div[1]/a/@href".format(i))[0]
                etherpad = 'https://etherpad.openeuler.org/p/{}-meetings'.format(sig_name)
                # Get the name, homepage and etherpad of all sigs
                sigs_list.append([sig_name, 'https://gitee.com' + sig_page, etherpad])
                i += 2
            sigs_list = sorted(sigs_list)
            print(sigs_list)
            for sig in sigs_list:
                # Gtt group_id
                sql = "select group_id from cve_git_repo_groups where group_name = %s"
                val = (sig[0],)
                group_id = mysql.getOne(sql, val)["group_id"]
                # Delete the data in cve_gite_repo_member
                # group_id
                sql1 = "delete from cve_gite_repo_member where group_id = %s"
                val1 = (group_id,)
                mysql.delete(sql1, val1)
                mysql.dispose()
                # Get owners
                url = 'https://gitee.com/openeuler/community/blob/master/sig/{}/OWNERS'.format(sig[0])
                r = requests.get(url)
                html = etree.HTML(r.text)
                res = html.xpath('//div[@class="line"]/text()')
                owners = []
                for i in res[1:]:
                    maintainer = i.strip().split('-')[-1].strip()
                    create_time = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
                    sql = "insert into cve_gite_repo_member (group_id,member_name," \
                          "member_type,create_time) values (%s, %s, %s, %s)"
                    val = (group_id, maintainer, "Maintainer", create_time)
                    mysql.insertOne(sql, val)
                    mysql.dispose()
                    owners.append(maintainer)
                print(owners)
            mysql.close()
